package com.l024.easysecurity.modules.sys.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.l024.easysecurity.common.page.PageData;
import com.l024.easysecurity.common.page.PageParams;
import com.l024.easysecurity.common.utils.R;
import com.l024.easysecurity.modules.sys.entity.SysMenu;
import com.l024.easysecurity.modules.sys.entity.SysRole;
import com.l024.easysecurity.modules.sys.service.SysRoleService;
import com.l024.easysecurity.modules.sys.vo.sys.SysRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 获取所有角色信息
     * @return
     */
    @GetMapping("/list")
    public R getSysRoles(){
        return new R().put("data",sysRoleService.getSysRoles());
    }

    /**
     * 获取所有角色和菜单信息
     * @return
     */
    @GetMapping("/menus")
    public R getRoleAllAndMenu(){
        return new R().put("data",sysRoleService.getRoleAllAndMenu());
    }

    /**
     * 分页条件查询角色和菜单
     */
    @PostMapping("/list")
    public R list(@RequestBody JSONObject param){
        int pageSize = 10;
        int pageNum = 1;
        SysRole sysRole = new SysRole();
        if(param!=null){
            if(param.containsKey("pageSize")){
                pageSize = param.getInteger("pageSize");
            }
            if(param.containsKey("pageNum")){
                pageNum = param.getInteger("pageNum");
            }

            if(param.containsKey("role")){
                JSONObject role = param.getJSONObject("role");
                if(role!=null){
                    sysRole = JSONObject.toJavaObject(role, SysRole.class);
                }
            }
        }
        PageData<SysRoleVo> sysRolePageData = sysRoleService.selectPageSysRole(sysRole, PageParams.build(pageSize, pageNum));
        return new R().put("data",sysRolePageData);
    }
    /**
     * 获取所有菜单信息
     */
    @GetMapping("/menus/all")
    public R getMenus(){
        List<SysMenu> sysMenus = sysRoleService.queryAllMenus();
        return R.ok().put("data",sysMenus);
    }

    /**
     * 添加角色信息
     */
    @PostMapping("/add")
    public R addRole(@RequestBody SysRoleVo sysRoleVo){
        return sysRoleService.addRole(sysRoleVo);
    }

    /**
     * 更新角色信息
     */
    @PostMapping("/update")
    public R updateRole(@RequestBody SysRoleVo sysRoleVo){
        return sysRoleService.updateRole(sysRoleVo);
    }

    /**
     * 删除角色信息
     */
    @GetMapping("/del/{roleId}")
    public R delRole(@PathVariable("roleId")long roleId){
        return sysRoleService.delRole(roleId);
    }
}
